package com.example.administrator.testz.News;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.example.administrator.testz.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private final String TAG = "TAGSS";
    private String URLPath = "http://www.imooc.com/api/teacher?type=4&num=30";
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.listView1);
        new NewsAsyncTask().execute(URLPath);
    }

    private List<NewsBean> getJsonData(String url){
        List<NewsBean> newslist = new ArrayList<>();
        try {
            String jsonStr = readStream(new URL(url).openStream());
            NewsBean bean;
            JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(jsonStr);
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                for (int i = 0; i < jsonArray.length(); i++){
                    jsonObject = jsonArray.getJSONObject(i);
                    Log.i(TAG, "jsonObject : " + jsonObject.toString());
                    bean = new NewsBean();
                    bean.setmPhotoURL(jsonObject.getString("picSmall"));
                    bean.setmContent(jsonObject.getString("description"));
                    bean.setmTitle(jsonObject.getString("name"));
                    newslist.add(bean);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newslist;
    }

    private String readStream(InputStream is){
        InputStreamReader isr = null;
        String result = "";
        try {
            String line = "";
            isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            while((line = br.readLine()) != null){
                result += line;
            }
            br.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>>{

        @Override
        protected List<NewsBean> doInBackground(String... params) {
            return getJsonData(params[0]);
        }

        @Override
        protected void onPostExecute(List<NewsBean> newsBeans) {
            super.onPostExecute(newsBeans);
            AdapterNews2 adapter = new AdapterNews2(MainActivity.this, newsBeans, mListView);
            mListView.setAdapter(adapter);
        }
    }

}
